//
//  PositionTableViewCell.m
//  RedManStock
//
//  Created by lanou3g on 15/8/1.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "PositionTableViewCell.h"
#import "TradeOperateViewController.h"
#import "MyCorePosition.h"
#import "ParameterHeader.h"
#import "MBProgressHUD.h"
@implementation PositionTableViewCell

- (void)awakeFromNib {
    // Initialization code
    [self.sellButton addTarget:self action:@selector(doSellTask:) forControlEvents:UIControlEventTouchUpInside];
}

//获得View所在的controller
- (UIViewController *)viewController{
    for (UIView *next = [self superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}

- (void)doSellTask:(UIButton *)button{
    //
    if ([self.position.availableVolume intValue]>0) {
        TradeOperateViewController *tradeOperateVC=[[TradeOperateViewController alloc]init];
        tradeOperateVC.sendCode=self.position.stockCode;
        tradeOperateVC.position=self.position;
        tradeOperateVC.buyOrSell=NO;
        [self.viewController.navigationController pushViewController:tradeOperateVC animated:YES];
    }
    else{
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.viewController.view animated:YES];
        hud.mode=MBProgressHUDModeText;
        hud.labelText=[NSString stringWithFormat:@"%@目前不能卖出",self.position.stockName];
        [hud hide:YES afterDelay:1.5f];
    }
    
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}


- (void)setPosition:(MyCorePosition *)position{
    if (_position!=position) {
        _position=position;
    }
    
    
    self.stockStrLabel.text=[NSString stringWithFormat:@"%@ %@",position.stockName,position.stockCode];
    self.nowPriceLabel.text=[NSString stringWithFormat:@"%@元",position.nowPrice];
    self.costPriceLabel.text=[NSString stringWithFormat:@"%@元",position.costPrice];
    if ([position.updownRate floatValue]>=0) {
        self.updownRateLabel.text=position.updownRate;
        self.updownRateLabel.textColor=MyRedColor;
    }
    else{
        self.updownRateLabel.text=position.updownRate;
        self.updownRateLabel.textColor=MyGreenColor;
    }
    self.nowTotalPriceLabel.text=[NSString stringWithFormat:@"%@元",position.nowTotalPrice];
    self.volumeLabel.text=[NSString stringWithFormat:@"%@股",position.volume];
    self.availableVolumeLabel.text=[NSString stringWithFormat:@"%@股",position.availableVolume];
}
@end
